
import { isEmpty } from 'shiptools'
import { compact } from '../../utils/index'
import { CAR_NO_DEFAULT_TRUCK, CAR_NO_DISABLED } from '../../utils/constants'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    code: {
      type: Array,
      default: CAR_NO_DEFAULT_TRUCK,
      observer: function (newVal, oldVal) { 
        if(newVal !== oldVal && !isEmpty(newVal)) {
          this.init(newVal)
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    carCode: '',

    keyBoardShow: false,
    
    keyBoardListArea: ['粤', '琼', '桂', '渝', '川', '贵', '豫', '湘', '浙', '京', '鄂', '黑', '赣', '苏', '冀', '闽', '云', '鲁', '临', '辽', '吉', '晋', '皖', '陕', '沪', '新', '甘', '蒙', '津', '青', '宁', '军', '藏', '警', '滇', '港', '黔', '澳', '使', '删除'],
    keyBoardList: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '澳', '港', '警', '删除'],

    keyBoardShow: false,
    needAdapter: false,
    current: 0,
    carNo: CAR_NO_DEFAULT_TRUCK,
    carNo02: '',
    carNo28: '',

    carNoDisabled: CAR_NO_DISABLED
  },

  /**
   * 组件的方法列表
   */
  methods: {
    init: function(code) {
      const carCode = code.join('').length === 0? '请输入车牌': code.join('')
      this.setData({
        carCode,
        carNo: code,
        carNo02: code.slice(0, 2),
        carNo28: code.slice(2, 8),
      })
    },

    onKeyBoardShow: function() {
      this.setData({
        keyBoardShow: true
      })
    },
    closeKyeBoard: function() {
      this.setData({
        keyBoardShow: false
      })

    },
    editCurrentInput: function(e) {
      const { current, carNo } = this.data;
      var val = e.currentTarget.dataset.val;
      if (
          (current == 0 && /^\d$/.test(val)) ||
          (current != -1 && CAR_NO_DISABLED.indexOf(val) > -1)
      ) {
          return;
      }
      if (val == "删除") {

          let _current = compact(carNo).length - 1
          if(_current > -1) {
              carNo[_current] = "";
          }
          var _current1 = (_current - 1) < 0 ? -1 : (_current - 1);
          this.setData({
              carNo,
              carNo02: carNo.slice(0, 2),
              carNo28: carNo.slice(2, 8),
              current: _current1
          });
          return;
      }
      let _current = compact(carNo).length
      var _current2 = (_current + 1) > 7 ? 7 : (_current + 1);
      
      if(_current < 8) {
          carNo[_current] = val;
      }
      this.setData({
          carNo,
          carNo02: carNo.slice(0, 2),
          carNo28: carNo.slice(2, 8),
          current: _current
      });
    },
    confirmKyeBoard: function(e) {
      const carNo = this.data.carNo
      this.setData({ keyBoardShow: false });
      this.triggerEvent('current', carNo)
    }
  }
})
